﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FinApp.ViewModels.Base;
using FinApp.DataProviders.Configuration;
using System.Runtime.Serialization.Json;
using FinApp.Model;
using System.Windows.Navigation;
using System.Globalization;
using Microsoft.Phone.Controls;

namespace FinApp.ViewModels {
    public class CreditCardViewModel : ViewModelBase {

        #region Members
        private string id;
        private string mode;
        private string issuer;
        private double balance;
        private Commands.Base.CommandBase paymentCommand;
        private DataContractJsonSerializer creditCardSerializer;
        private string commercePasswordKey;
        private string commerceSaltKey;

        private string commerceId;
        private double quantity;
        private string password;
        private string salt;
        private string checkField;

        #endregion

        #region Properties
        /// <summary>
        /// Obtiene o establece el identificador de la tarjeta
        /// </summary>
        public string Id {
            get { return id; }
            set { 
                id = value;
                this.NotifyPropertyChanged("Id");
            }
        }
        /// <summary>
        /// Obtiene o establece el modo de la tarjeta
        /// </summary>
        public string Mode {
            get { return mode; }
            set { 
                mode = value;
                this.NotifyPropertyChanged("Mode");
            }
        }
        /// <summary>
        /// Obtiene o establece el tipo de tarjeta
        /// </summary>
        public string Issuer {
            get { return issuer; }
            set {
                issuer = value;
                this.NotifyPropertyChanged("Issuer");
            }
        }
        /// <summary>
        /// Obtiene el total de dinero disponible
        /// </summary>
        public double Balance {
            get { return balance; }
            set { 
                balance = value;
                this.NotifyPropertyChanged("Balance");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public ICommand PaymentCommand {
            get { return paymentCommand; }
        }

        public string CommerceId {
            get { return commerceId; }
            set { commerceId = value; }
        }

        public double Quantity {
            get { return quantity; }
            set { quantity = value; }
        }

        public string Password {
            get { return password; }
            set { password = value; }
        }

        public string Salt {
            get { return salt; }
            set { salt = value; }
        }

        public string CheckField {
            get { return checkField; }
            set { checkField = value; }
        }

        public string CommercePasswordKey {
            get { return commercePasswordKey; }
            set { commercePasswordKey = value; }
        }

        public string CommerceSaltKey {
            get { return commerceSaltKey; }
            set { commerceSaltKey = value; }
        }
        #endregion

        #region Events
        private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
            
            WebClient webClient = sender as WebClient;
            webClient.OpenReadCompleted -= new OpenReadCompletedEventHandler(OnOpenReadCompleted);
            CardPaymentModel payment = (CardPaymentModel)creditCardSerializer.ReadObject(e.Result);
            //payment.CardPaymentDataModel.Code

            var res = App.Current.RootVisual as PhoneApplicationFrame;
            res.Navigate(new Uri(string.Format("/Views/QRViewerPage.xaml?CommerceId={0}&Quantity={1}&CheckField={2}&Password={3}&Salt={4}&Token={5}", Uri.EscapeDataString(CommerceId), Uri.EscapeDataString(Quantity.ToString(CultureInfo.InvariantCulture)), Uri.EscapeDataString(CheckField), Uri.EscapeDataString(CommercePasswordKey), Uri.EscapeDataString(CommerceSaltKey), Uri.EscapeDataString(payment.CardPaymentDataModel.Code)), UriKind.Relative));

        }
        #endregion

        public CreditCardViewModel(){
            this.creditCardSerializer = new DataContractJsonSerializer(typeof(CardPaymentModel));
            this.paymentCommand = new Commands.Base.CommandBase(() => {
                
                WebClient webClient = new WebClient();
                webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OnOpenReadCompleted);
                webClient.OpenReadAsync(new Uri(ConfigurationProvider.Instance.GetApiLoggedInUrl() + string.Format("/operations/payment/{0}/code?value={1}", this.Id, this.Quantity)));

            });
        }

        public override void LoadData() {
            throw new NotImplementedException();
        }

    }
}
